package com.tlcx.kfip.ui;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tlcx.kfip.R;

/**
 * 自定义倒计时控件
 * Created by victor on 2016/11/16 11:15.
 * Email:zhuzhaoyang@yuanchuangyun.com
 */

public class CustomCountDownView extends LinearLayout{

    private TextView mShowTimeTv;
    private RelativeLayout mLayout;
    private CustomCountDownTimer countDownTimer;
    private OnCountDownFinishListener onFinishListener;

    public CustomCountDownView(Context context) {
        super(context,null);
        initView(context);
    }

    public CustomCountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    private void initView(Context context){
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.widget_custom_count_down,null,false);
        addView(view);
        mShowTimeTv = (TextView) view.findViewById(R.id.tv_widget_count_down);
        mLayout = (RelativeLayout) view.findViewById(R.id.rl_widget_custom_count_down);
    }
    /**
     * 设置倒计时总时间及倒计时结束时回调
     * @param millisInFuture  总时间
     */
    public void setCountDownTimer(long millisInFuture,OnCountDownFinishListener onFinishListener){
        //为了显示毫秒，所以0.05秒倒计时一次
        countDownTimer = new CustomCountDownTimer(millisInFuture,50);
        this.onFinishListener = onFinishListener;
        countDownTimer.start();
    }
    public class CustomCountDownTimer extends CountDownTimer{

        /**
         * @param millisInFuture  倒计时总时间  单位是毫秒
         * @param countDownInterval   倒计时间隔多长时间调一次onTick()方法
         */
        public CustomCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            int ss = 1000;
            int mi = ss * 60;
            long minute = l / mi;                   //计算分钟
            long second = (l - minute * mi) / ss;       //得到秒数
            long milliSecond = l - minute * mi - second * ss;  //得到毫秒数
            String strMinute = minute < 10 ? "0" + minute : "" + minute;  //分钟
            String strSecond = second < 10 ? "0" + second : "" + second;  //秒
            String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;  //毫秒数
            strMilliSecond = milliSecond > 100 ? strMilliSecond.substring(0,
                    strMilliSecond.length() - 1) : "" + strMilliSecond;//毫秒超过100显示前2位
            String str = strMinute+":"+strSecond+":"+strMilliSecond;
            if (mShowTimeTv != null){
                if (mLayout != null && mLayout.getVisibility()!= View.VISIBLE)
                    mLayout.setVisibility(View.VISIBLE);
                mShowTimeTv.setText(str);
            }
        }

        @Override
        public void onFinish() {

            if (countDownTimer != null){
                countDownTimer.cancel();
            }
            //倒计时结束
            if (mLayout!=null){
                mLayout.setVisibility(View.GONE);
            }
            if (onFinishListener != null) {
                onFinishListener.onFinish();
            }
        }
    }
    public interface OnCountDownFinishListener{
        void onFinish();
    }
}
